# - Find Thrift C library (c_glib) (a cross platform RPC lib/tool)
# This module defines
#  THRIFT_C_GLIB_VERSION_STRING, version string of ant if found
#  THRIFT_C_GLIB_LIBRARIES, static or shared libraries to link
#  THRIFT_C_GLIB_INCLUDE_DIR, where to find THRIFT headers
#  THRIFT_C_GLIB_COMPILER, thrift compiler executable
#  THRIFT_C_GLIB_FOUND, If false, do not try to use ant
#
# Imported target:
#  thrift_c_glib-static
#
# Function:
#  thrift_gen_c_glib(<path to thrift file> <output variable with file list>)
# 
# Variablies to set:
#  THRIFT_C_GLIB_HOME thrift c library install location
#  and envoronment variable THRIFT_C_GLIB_HOME
# 2018 - modified by guyadong

# FindBoost.cmake supported imported targets after cmake version 3.5
cmake_minimum_required( VERSION 3.5 )
# prefer the thrift version supplied in THRIFT_C_GLIB_HOME (cmake -DTHRIFT_C_GLIB_HOME then environment)
find_path(THRIFT_C_GLIB_INCLUDE_DIR
    NAMES
        thrift/c_glib/thrift.h
    HINTS
        ${THRIFT_C_GLIB_HOME}
        ENV THRIFT_C_GLIB_HOME
        /usr/local
        /opt/local
    PATH_SUFFIXES
        include
)
macro(FIND_THRIFT_C_GLIB_LIBRARY var)
	set(_find_lib_suffix_bak ${CMAKE_FIND_LIBRARY_SUFFIXES})
	set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
	# prefer the thrift version supplied in THRIFT_C_GLIB_HOME
	find_library(${var}
	    NAMES
	        libthrift_c_glib
	    HINTS
	        ${THRIFT_C_GLIB_HOME}
	        ENV THRIFT_C_GLIB_HOME
	        /usr/local
	        /opt/local
	    PATH_SUFFIXES
	        lib lib64
	)
	set(CMAKE_FIND_LIBRARY_SUFFIXES ${_find_lib_suffix_bak})
endmacro(FIND_THRIFT_C_GLIB_LIBRARY)

# 查找thrift依赖库 boost
# 如果相关依赖库找不到则报错退出
macro(FIND_THRIFT_C_GLIB_DEPENDENCIES)
	# 依赖库的include文件夹
	set(THRIFT_C_GLIB_DEPENDENT_DIRS)
	# 依赖的库
	set(THRIFT_C_GLIB_DEPENDENT_LIBRARIES)
	
	find_package(GLIB REQUIRED COMPONENTS gobject)
	list(APPEND THRIFT_C_GLIB_DEPENDENT_DIRS ${GLIB_INCLUDE_DIRS})
	list(APPEND THRIFT_C_GLIB_DEPENDENT_LIBRARIES ${GLIB_LIBRARIES} ${GLIB_GOBJECT_LIBRARIES} $<$<C_COMPILER_ID:GNU>:-lpthread >)
endmacro(FIND_THRIFT_C_GLIB_DEPENDENCIES)

# prefer default thrift library
FIND_THRIFT_C_GLIB_LIBRARY(THRIFT_C_GLIB_LIBRARIES libthrift_c_glib)
find_program(THRIFT_C_GLIB_COMPILER
    NAMES
        thrift
    HINTS
        ${THRIFT_C_GLIB_HOME}
        ENV THRIFT_C_GLIB_HOME
        /usr/local
        /opt/local
    PATH_SUFFIXES
        bin bin64
)

if (THRIFT_C_GLIB_COMPILER)
    exec_program(${THRIFT_C_GLIB_COMPILER}
        ARGS -version OUTPUT_VARIABLE __thrift_OUT RETURN_VALUE THRIFT_RETURN)
    string(REGEX MATCH "[0-9]+.[0-9]+.[0-9]+-[a-z]+$" THRIFT_C_GLIB_VERSION_STRING ${__thrift_OUT})

    # define utility function to generate cpp files
    function(thrift_gen_c_glib thrift_file THRIFT_C_FILES_LIST THRIFT_GEN_INCLUDE_DIR)
        set(_res)
        set(_res_inc_path)
        if(EXISTS ${thrift_file})
            get_filename_component(_target_dir ${thrift_file} NAME_WE)
            message("thrif_gen_cpp: ${thrift_file}")

            if(NOT EXISTS ${CMAKE_BINARY_DIR}/${_target_dir})
                file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/${_target_dir})
            endif()
            exec_program(${THRIFT_C_GLIB_COMPILER}
                ARGS -o "${CMAKE_BINARY_DIR}/${_target_dir}" --gen c_glib ${thrift_file}
                OUTPUT_VARIABLE __thrift_OUT
                RETURN_VALUE THRIFT_RETURN)
            file(GLOB_RECURSE __result_src "${CMAKE_BINARY_DIR}/${_target_dir}/*.c")
            file(GLOB_RECURSE __result_hdr "${CMAKE_BINARY_DIR}/${_target_dir}/*.h")
            list(APPEND _res ${__result_src})
            list(APPEND _res ${__result_hdr})
            if(__result_hdr)
                list(GET __result_hdr 0 _res_inc_path)
                get_filename_component(_res_inc_path ${_res_inc_path} DIRECTORY)
            endif()
        else()
            message("thrift_gen_c_glib: file ${thrift_file} does not exists")
        endif()
        set(${THRIFT_C_FILES_LIST} "${_res}" PARENT_SCOPE)
        set(${THRIFT_GEN_INCLUDE_DIR} "${_res_inc_path}" PARENT_SCOPE)
    endfunction()
endif ()


include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(THRIFTCGLIB DEFAULT_MSG THRIFT_C_GLIB_LIBRARIES THRIFT_C_GLIB_INCLUDE_DIR THRIFT_C_GLIB_COMPILER)
mark_as_advanced(THRIFT_C_GLIB_LIBRARIES THRIFT_C_GLIB_INCLUDE_DIR THRIFT_C_GLIB_COMPILER THRIFT_C_GLIB_VERSION_STRING)

############################
function(set_thrift_c_glib_imported_target target imported_location)
	if(NOT TARGET ${target})
		message(FATAL_ERROR "${target} is invalid target")
	endif()
	if(NOT EXISTS ${imported_location})
		message(FATAL_ERROR "${imported_location} is invalid library")
	endif()
	set_target_properties(${target} PROPERTIES
	  IMPORTED_LINK_INTERFACE_LANGUAGES "C"
	  INTERFACE_INCLUDE_DIRECTORIES "${THRIFT_C_GLIB_INCLUDE_DIR};${THRIFT_C_GLIB_DEPENDENT_DIRS}"
	  IMPORTED_LOCATION "${imported_location}"
	  )

  if(THRIFT_C_GLIB_DEPENDENT_LIBRARIES)
		set_target_properties(${target} PROPERTIES
			IMPORTED_LINK_INTERFACE_LIBRARIES "${THRIFT_C_GLIB_DEPENDENT_LIBRARIES}"
		  )
	endif()
endfunction(set_thrift_c_glib_imported_target imported_location)

if(THRIFTCGLIB_FOUND)
	set(ThriftCGLIB_FOUND TRUE)
	
	FIND_THRIFT_C_GLIB_DEPENDENCIES()
	add_library(thrift_c_glib-static STATIC IMPORTED)

	set_thrift_c_glib_imported_target(thrift_c_glib-static ${THRIFT_C_GLIB_LIBRARIES})

endif()

unset(BOOST_REQUIRED_COMPONENTS)
unset (THRIFT_C_GLIB_DEPENDENT_DIRS)
unset(THRIFT_C_GLIB_DEPENDENT_LIBRARIES)